<?php
  include('header.php');
  $hide_footer = -1;
  $ascdesc = "ASC";
  $sort = "LOWER(user_name)";
  $sorting = "name";
  $result = $db->query("SELECT COUNT(*) FROM " . $table_prefix . "users");
  if (!$result) {
      echo "$l_error_occured";
  } else {
      $qqq += 1;
      $r = $db->fetch_row($result);
      $numrows = $r[0] - 1;
      $rowsperpage = $users_per_page;
      $totalpages = ceil($numrows / $rowsperpage);
      if (isset($_GET['page']) && is_numeric($_GET['page'])) {
          $currentpage = (int)$_GET['page'];
      } else {
          $currentpage = 1;
      }
      if ($currentpage > $totalpages) {
          $currentpage = $totalpages;
      }
      if ($currentpage < 1) {
          $currentpage = 1;
      }
      $offset = ($currentpage - 1) * $rowsperpage;
      $current_page = $page;
      if ($current_page == "") {
          $current_page = 1;
      }
      $range = $max_pagination_link;
      if ($range >= $totalpages) {
          $range = $totalpages;
      }
	  if (isset($_GET['order']))
	  {
      if (mysql_real_escape_string($_GET['order']) == "") {
          $ascdesc = "DESC";
      }
      if (mysql_real_escape_string($_GET['order']) == "ASC") {
          $ascdesc = "ASC";
      }
      if (mysql_real_escape_string($_GET['order']) == "DESC") {
          $ascdesc = "DESC";
      }
      }
	    if (isset($_GET['sort'])) {   
      if (mysql_real_escape_string($_GET['sort']) == "" 
	  or $_GET['sort'] !="name" 
	  or $_GET['sort'] !="joined"
	  or $_GET['sort'] !="posts"
	  or $_GET['sort'] !="last_login") {
          $sort = "LOWER(user_name)";
          $sorting = "name";
      }

      if (mysql_real_escape_string($_GET['sort']) == "name") {
          $sort = "LOWER(user_name)";
          $sorting = "name";
      }
      if (mysql_real_escape_string($_GET['sort']) == "joined") {
          $sort = "user_date";
          $sorting = "joined";
      }
      if (mysql_real_escape_string($_GET['sort']) == "posts") {
          $sort = "user_posts";
          $sorting = "posts";
      }
      if (mysql_real_escape_string($_GET['sort']) == "last_login") {
          $sort = "user_last_login";
          $sorting = "last_login";
      }
      
      	  }
		  if (isset($_GET['order'])){
      if (mysql_real_escape_string($_GET['order']) == "") {
          $ascdesc = "ASC";
      }
      if (mysql_real_escape_string($_GET['order']) == "ASC") {
          $ascdesc = "DESC";
      } else {
          $ascdesc = "ASC";
      }
	  }
	  /* TO DO: FIX TOP PAGINATION SORT
      echo "<table><tr>";
      echo '<td align="right" class="pagination"> <b>' . $l_page . ' ' . $current_page . ' ' . $l_page_of . ' ' . $totalpages . '</b> [' . $numrows . ' users]  &nbsp; &nbsp;';
      if ($currentpage > 1) {
          echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?order=" . $ascdesc . "&sort=$sorting&page=1'><<</a> ";
          $prevpage = $currentpage - 1;
          echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?order=" . $ascdesc . "&sort=$sorting&page=$prevpage'><</a> ";
      }
      for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
          if (($x > 0) && ($x <= $totalpages)) {
              if ($x == $currentpage) {
                  echo " [<b>$x</b>] ";
              } else {
                  echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?order=" . $ascdesc . "&sort=$sorting&page=$x'>$x</a> ";
              }
          }
      }
      if ($currentpage != $totalpages) {
          $nextpage = $currentpage + 1;
          echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=$nextpage'>></a> ";
          echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=$totalpages'>>></a> ";
      }
      echo '</td></tr></table>';
	  TO DO: FIX TOP PAGINATION SORT */ 
      
      if ($page <= 0 or $page > $totalpages) {
          $page = 1;
      }
      
      $offset = $users_per_page * ($page - 1);
      
      $username = "<a style='text-decoration: underline' href='members.php?order=$ascdesc&sort=name'>$l_un2</a>";
      $joined = "<a style='text-decoration: underline' href='members.php?order=$ascdesc&sort=joined'>$l_joined2</a>";
      $posts = "<a style='text-decoration: underline' href='members.php?order=$ascdesc&sort=posts'>$l_sposts</a>";
      $last_login = "<a style='text-decoration: underline' href='members.php?order=$ascdesc&sort=last_login'>$l_last_login</a>";
      
      $qqq += 1;
      $tags = array('{USERNAME}', '{JOINED}', '{POSTS}', '{LAST_LOGIN}','{EMAIL}','{WWW}','{MSN}','{AGE}');
      $data = array($username, $joined, $posts, $last_login,$l_email,$l_www,$l_msn,$l_age);
      echo str_replace($tags, $data, file_get_contents("./style/$default_style/members.html"));
      $pageTitle = 'Members';
      $result = $db->query("SELECT * FROM " . $table_prefix . "users WHERE user_name !='Anonymus' ORDER BY $sort " . $ascdesc . " LIMIT $offset, $users_per_page");
      
      while ($list = $db->fetch_array($result,'assoc')) {
          $tag1 = "";
          $tag2 = "";
          $user = $list['user_name'];
          $user = "<a href='user.php?u=" . $list['user_id'] . "'>" . $list['user_name'] . "</a>";
          if ($list['user_id'] == 1) {
              $user = "</b><i>" . $list['user_name'] . "</i>";
          }
          $last_login = date($date_format, strtotime($list['user_last_login']));
          
          if (date("Y", strtotime($list['user_last_login'])) <= "1990") {
              $last_login = "";
          }
          if (date("d.m.Y", strtotime($list['user_last_login'])) == $date->format("d.m.Y")) {
              $last_login = "<b>$l_today</b>, " . date("H:i", strtotime($list['user_last_login'])) . "";
          }
		  
		  if (date("d.m.Y", strtotime($list['user_last_login'])) == date("d.m.Y",$yesterday)) {
              $last_login = "<b>$l_yesterday</b>, " . date("H:i", strtotime($list['user_last_login'])) . "";
          }
		  
          if ($list['is_banned'] == 1) {
		   $user = "<a href='user.php?u=" . $list['user_id'] . "'><span style = 'color:#AAAAAA'>" . $list['user_name'] . "</span></a>";
		  }

		  $user_gender = $list['user_gender'];
	      $gender_image = "";
	      if ($user_gender == 1) 
	      {
	      $gender_image = "<img title='$l_male' align='absmiddle' src='./style/$default_style/img/male.gif'>";
	      };
	      if ($user_gender == 2) 
	      {
          $gender_image = "<img title='$l_female' align='absmiddle' src='./style/$default_style/img/female.gif'>";
          };
          $total_users = mysql_num_rows($result);
		  
		  $user_email ="";
		  $user_www = "";
		  $user_msn = "";
		  $user_age = "";
	  if ($list['show_email']==1) {$user_email = "<a title='$list[user_name] email: $list[user_email]' href='mailto:$list[user_email]'><img src='./style/$default_style/img/email.png' align='absmiddle' border=0 ></a>";}
		  
		if ($list['user_www']!="") {$user_www = "<a title='$list[user_www]' href='$list[user_www]' target='_blank'><img src='./style/$default_style/img/url.png' align='absmiddle' border=0 ></a>";}
		  
		  if ($list['user_msn']!="") {$user_msn = "<a title='$list[user_msn]' href='$list[user_msn]' target='_blank'><img src='./style/$default_style/img/msn.png' align='absmiddle' border=0 ></a>";}
		  
		  $show_age = $list['user_show_age'];
		  if (strstr($list['user_birthday'],"-"))
		  {
		  $parts=explode("-", $list['user_birthday']);
	      $yyyy = ($parts[2]);
		  }
	      $tdage = ""; $age = "";
		  if ($show_age == 1 AND $yyyy!=0) {$age = date("Y")-$yyyy;}
          $tags = array('{USER}', '{USER_DATE}', '{USER_POSTS}', '(TAG1)', '(TAG2)', '{LAST_LOGIN}','{GENDER}','{EMAIL}','{WWW}','{MSN}','{AGE}');
		  
		  $user_joined = date($date_format, strtotime($list['user_date']));
		  
		  if (date("d.m.Y", strtotime($list['user_date'])) == $date->format("d.m.Y"))
		  {
			$user_joined = "<b>$l_today</b>, " . date("H:i:s", strtotime($list['user_date']));
				  }
			if (date("d.m.Y", strtotime($list['user_date'])) == date("d.m.Y",$yesterday)) {
                  $user_joined = "<b>$l_yesterday</b>, " . date("H:i", strtotime($list['user_date'])) . "";
                   } 
		  
		  
          $data = array($user, $user_joined, $list['user_posts'], $tag1, $tag2, $last_login,$gender_image,$user_email,$user_www,$user_msn,$age);
          echo str_replace($tags, $data, file_get_contents("./style/$default_style/members_row.html"));
      }
  }
  echo "<table><tr>";
  if (isset($_GET['page']) && is_numeric($_GET['page'])) {
      $currentpage = (int)$_GET['page'];
  } else {
      $currentpage = 1;
  }
  if ($currentpage > $totalpages) {
      $currentpage = 1;
  }
  if ($currentpage < 1) {
      $currentpage = 1;
  }
  $offset = ($currentpage - 1) * $rowsperpage;

  if ($current_page == "") {
      $current_page = 1;
  }
  $range = $max_pagination_link;
  if ($range >= $totalpages) {
      $range = $totalpages;
  }
  if (isset($_GET['order']))
  {
  if (mysql_real_escape_string($_GET['order']) == "ASC") {
      $ascdesc = "ASC";
  }
  if (mysql_real_escape_string($_GET['order']) == "DESC") {
      $ascdesc = "DESC";
  }
  }
  echo '<td align="right" class="pagination"> <b>' . $l_page . ' ' . $current_page . ' ' . $l_page_of . ' ' . $totalpages . '</b> [' . $numrows . ' '.$l_users_found.']  &nbsp; &nbsp;';
  if ($currentpage > 1) {
      echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=1'><<</a> ";
      $prevpage = $currentpage - 1;
      echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=$prevpage'><</a> ";
  }
  for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
      if (($x > 0) && ($x <= $totalpages)) {
          if ($x == $currentpage) {
              echo " [<b>$x</b>] ";
          } else {
              echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=$x'>$x</a> ";
          }
      }
  }
  if ($currentpage != $totalpages) {
      $nextpage = $currentpage + 1;
      echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=$nextpage'>></a> ";
      echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=$totalpages'>>></a> ";
  }
  echo '</td></tr></table>';
  if ($hide_footer < 1) {
      include 'footer.php';
  }
  $pageContents = ob_get_contents();
  ob_end_clean();
  echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
?>